#-*- encoding:utf-8 -*-
from peer import *

__author__  = "Number Huang<huang.bafeng@iNeed.com>"
__status__  = "developing"
__version__ = "0.1108.31.1"



class Passenger(Peer):
    """

    1, publish/unpublish service
    2, handle the CO,TOGD request
    3, handle the response from gui
    """
    def __init__(self,owner):

        self.role= EPR_PASSENGER
        self.log = owner.log
        super(Passenger,self).__init__(owner)
        self.listenport = passengerAddr[1]
        self.notifyRequestMap[CSTR_CO]=self._handleConfirmOrderRequest
        self.notifyRequestMap[CSTR_RB]=self._handleTaxiArrived
        self.notifyRequestMap[CSTR_OC]=self._handleCancelOrderRequest
        self.sessionID = passengerFirstSession
       

    def _handleCancelOrderRequest(self,session,params):
        serviceDict     = getServiceByDict(params)
        pubID           = serviceDict["pubID"]
        curService      = self.queryService(pubID)
        if curService:
            curService.updateStatus(ESE_WF_GUI_TOGD)
        
    def _handleConfirmOrderRequest(self,session,params):
        serviceDict     = getServiceByDict(params)
        pubID           = serviceDict["pubID"]
        curService      = self.queryService(pubID)
        curService.orderDict = serviceDict
        self._sayHello2Driver(EN_OK,retCodeMap[EN_OK],{"params":params})
        curService.updateStatus(ESE_ORDER_DONE)
        self.updateStatus(ESE_ORDER_DONE)
        session.sendStandyResponse("NOTIFY",params[3], EN_OK,retCodeMap[EN_OK])
        #reporter        = ConfirmDriverReporter(params,None,self._sayHello2Driver)
        #self._handleStandbyRequest(CSTR_CO,session,params,CO_UI_TIMEOUT,reporter)
        #create the connection between driver and passenger
        
    def _sayHello2Driver(self,retCode,retInfo,extraInfo):
        """
        if the passenger confirmed the order,then say Hello to the driver to create the session
        between peers
        """
        if retCode==EN_OK:
            params = extraInfo["params"]
            serviceDict     = getServiceByDict(params)
            pubID = serviceDict["pubID"]
            peerPhone = serviceDict["driverID"]
            try:
                session = self.msgProxy.getSessionByPhone(peerPhone)
                if not session:
                    peerIP    = getIP(serviceDict["driverIP"])
                    session = self.msgProxy.createClientSession(peerIP)
                    session.peer.setPhone(peerPhone)
                session.setPubID(pubID)
                curService = self.queryService(pubID)
                curService.session = session
                self.getPeerPos(peerPhone)
            except:
                self.log.exception("error")
                raise
        
   
    def _handleTaxiArrived(self,session,params):
        #serviceDict     = getServiceByDict(params)
        #pubID = serviceDict["pubID"]
        #curService = self.queryService(pubID)
        #curService.updateStatus(ESE_READY_BORDING)
        #return EN_OK,retCodeMap[EN_OK]
        self.updateStatus(ESE_READY_BORDING)
        self._handleStandbyRequest(CSTR_RB,session,params,RB_UI_TIMEOUT)
        
    def comment(self,service,paramDict):
        params = self.mainFrom2Head 
        params +="Action: %s\r\n"%CSTR_CM
        params +="pubID: %d\r\n"%service.pubID
        params +="score: %d\r\n"%paramDict["score"]
        params +="comment: %s\r\n"%paramDict["comment"]
        
        flag = getSpecialFlag(CSTR_CM)
        self._sendRequest(self.mainSession,"NOTIFY",params,None,flag)
        service.updateStatus(ESE_TRANSATION_DONE)
        self.updateStatus(ES_IDLE)
        self.deleteService(None,service)
        
def testLogin(peer):
     user = raw_input("username:")
     if len(user)<2:
        user = '18917900803'
     pwd = 'fdd'
     peer.logIn(user,pwd,centerID,centerAddr)

if __name__=="__main__":
    try:
         passenger = Passenger(passengerAddr)

         while True:
            cmd = raw_input("cmd(login/publish/):")
    except:
         logging.exception("exit")